package com.sbeam.controller;

import com.sbeam.pojo.Games;
import com.sbeam.service.GamesService;
import com.sbeam.vo.GameVO;
import com.sbeam.vo.PageResult;
import com.sbeam.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/games")
public class GamesController {

    @Autowired
    private GamesService gamesService;

    @GetMapping("/getGamesList")
    public SysResult getGamesList(){
        List<Games> games = gamesService.getGamesList();
        if (games.isEmpty()){
            return SysResult.failed();
        }
        return SysResult.success(games);
    }

    @GetMapping("/getGamesList2")
    public SysResult getGamesList(PageResult pageResult){
        pageResult = gamesService.getGamesList(pageResult);
        return SysResult.success(pageResult);
    }

    @PutMapping("/updateGamesStatus")
    public SysResult updateGamesStatus(@RequestBody Games games){
        int result = gamesService.updateGamesStatus(games);
        if (result == 0){
            return SysResult.failed();
        }
        return SysResult.success();
    }

    @GetMapping("/searchByName")
    public SysResult searchByName(PageResult pageResult){
        pageResult = gamesService.searchByName(pageResult);
        if (pageResult.getRows()==null){
            return SysResult.failed();
        }
        return SysResult.success(pageResult);
    }

    @GetMapping("/getGamesById")
    public SysResult getGamesById(Integer id){
        Games games = gamesService.getGamesById(id);
        if (games == null){
            return SysResult.failed();
        }
        return SysResult.success(games);
    }

    @PutMapping("/updateGamesById")
    public SysResult updateGamesById(@RequestBody Games games){
        int result = gamesService.updateGamesById(games);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @DeleteMapping("/deleteGamesById")
    public SysResult deleteGamesById(Integer id){
        int result = gamesService.deleteGamesById(id);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @PostMapping("/addGames")
    public SysResult addGames(@RequestBody Games games){
        int result = gamesService.addGames(games);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @GetMapping("/getGameModel")
    public SysResult getGameModel(Integer id,String username){
        GameVO gameModel = gamesService.getGameModel(id,username);
        if (gameModel==null){
            return SysResult.failed();
        }
        System.out.println(gameModel);
        return SysResult.success(gameModel);
    }
}
